package com.notary.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.notary.entity.vo.JsonResult;
import com.notary.entity.ScheduleJob;
import com.notary.service.ScheduleJobService;
import com.notary.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务
 *
 * @author
 */
@Api(tags = "定时任务控制器")
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {


    private ScheduleJobService scheduleJobService;

    @Autowired
    public ScheduleJobController(ScheduleJobService scheduleJobService){
        this.scheduleJobService = scheduleJobService;
    }

    /**
     * 定时任务列表
     */
    @PostMapping("/list")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:list')")
    public JsonResult list(@RequestParam Map<String, Object> params) {
        IPage<ScheduleJob> page = scheduleJobService.queryPage(params);
        return JsonResult.success(page);
    }

    /**
     * 定时任务信息
     */
    @GetMapping("/info/{jobId}")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:info')")
    public JsonResult info(@PathVariable("jobId") Long jobId) {
        ScheduleJob schedule = scheduleJobService.getById(jobId);
        return JsonResult.success(schedule);
    }

    /**
     * 保存定时任务
     */
    @PostMapping("/save")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:save')")
    public JsonResult save(@RequestBody ScheduleJob scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);
        scheduleJobService.saveJob(scheduleJob);
        return JsonResult.success();
    }

    /**
     * 修改定时任务
     */
    @PostMapping("/update")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:update')")
    public JsonResult update(@RequestBody ScheduleJob scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);
        scheduleJobService.update(scheduleJob);
        return JsonResult.success();
    }

    /**
     * 删除定时任务
     */
    @PostMapping("/delete")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:delete')")
    public JsonResult delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return JsonResult.success();
    }

    /**
     * 立即执行任务
     */
    @PostMapping("/run")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:run')")
    public JsonResult run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return JsonResult.success();
    }

    /**
     * 暂停定时任务
     */
    @PostMapping("/pause")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:pause')")
    public JsonResult pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return JsonResult.success();
    }

    /**
     * 恢复定时任务
     */
    @PostMapping("/resume")
    //     @PreAuthorize("hasAnyAuthority(''sys:schedule:resume')")
    public JsonResult resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return JsonResult.success();
    }

}
